package com.bangcle.library.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class PixelImgView extends ImageView{
    private Bitmap mPixelBitmap=null;
    private OnPixelClickListener mOnPixelClickListener=null;
    private PointF mPoint = new PointF(0, 0);
    public PixelImgView(Context context) {
        super(context);
        this.initialize(context, null, 0);
    }
    public PixelImgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initialize(context, attrs, 0);
    }
    public PixelImgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initialize(context, attrs, defStyleAttr);
    }
    protected void initialize(Context context, AttributeSet attrs, int defStyleAttr) {
        super.setScaleType(ImageView.ScaleType.FIT_XY);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            mPoint.set(event.getX(),event.getY());
        }else if(event.getAction()==MotionEvent.ACTION_MOVE){
        }else if(event.getAction()==MotionEvent.ACTION_UP){
            trackTouchEvent(event);
            mPoint.set(0,0);
        }
        return true;
    }
    public boolean trackTouchEvent(MotionEvent event) {
        //未注册监听或事件映射图，则不处理
        if(mOnPixelClickListener==null||mPixelBitmap==null){
            return false;
        }
        float distance=(event.getX()-mPoint.x)*(event.getX()-mPoint.x)+(event.getX()-mPoint.x)*(event.getX()-mPoint.x);
        float maxDistance=100*100;
        //短按事件
        if(distance>maxDistance){
            return false;
        }
        //只处理弹起事件
        if(event.getAction()!=MotionEvent.ACTION_UP){
            return false;
        }
        int viewWidth=getWidth(),viewHeight=getHeight(),bitmapWidth=mPixelBitmap.getWidth(),bitmapHeight=mPixelBitmap.getHeight();
        //View或Bitmap长宽不合法，则不处理
        if(viewWidth<=0|viewHeight<=0||bitmapWidth<=0||bitmapHeight<=0){
            return false;
        }
        float posX=(event.getX()/viewWidth)*bitmapWidth;
        float posY=(event.getY()/viewHeight)*bitmapHeight;
        //事件坐标不合法，则不处理
        if(posX<0||posX>=bitmapWidth||posY<0||posY>=bitmapHeight){
            return false;
        }
        int color=mPixelBitmap.getPixel((int)posX,(int)posY);
        Log.e("PixelImgView",String.format("OnPixelClick(color=0x%X)",color));
        mOnPixelClickListener.OnPixelClick(color);
        return true;
    }


    public void setOnPixelClickListener(int mPixelImgResId,OnPixelClickListener mOnPixelClickListener) {
        try {
            mPixelBitmap=BitmapFactory.decodeResource(getResources(), mPixelImgResId);
            this.mOnPixelClickListener=mOnPixelClickListener;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static interface OnPixelClickListener{
        public void OnPixelClick(int color);
    }
}
